package com.aaa.controller;

import com.aaa.service.UserInfoService;
import com.aaa.service.impl.UserInfoServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/7/15 9:35
 * @description：用户信息的控制器
 * @modified By：
 * @version: 1.0
 */
@WebServlet(urlPatterns = "/user")
public class UserInfoController extends HttpServlet {
    UserInfoService userinfoservice = new UserInfoServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if (method == null) {
            method = method + "";
        }
        //将所有的请求参数和值放到一个集合中，key-value
        //parameterNames所有参数的名字
        Enumeration<String> parameterNames = req.getParameterNames();
        Map paramMap = new HashMap();
        while (parameterNames.hasMoreElements()) {
            String param = parameterNames.nextElement();
            Object value = req.getParameter(param);
            paramMap.put(param, value);

        }
        switch (method) {
            case "delete":
                //根据id删除人员
                String idStr = req.getParameter("id");
                //将字符串转换成int
                int id = Integer.parseInt(idStr);
                boolean b = userinfoservice.deleteUserById(id);
                if (b) {
                    //成功
                    findAll(req, resp);
                } else {
                    //失败
                    req.getRequestDispatcher("fail.jsp").forward(req, resp);
                }
                break;
            case "toAddPage":
                //去添加页面
                req.getRequestDispatcher("add.jsp").forward(req, resp);
                break;
            case "toTestAjaxPage":
                //去添加页面
                req.getRequestDispatcher("TestAjax.jsp").forward(req, resp);
                break;
            case "add":
                //补充创建的时间
                paramMap.put("createtime",new Date());
                boolean add = userinfoservice.addUser(paramMap);
                if (add) {
                    //成功
                    findAll(req, resp);
                } else {
                    //失败
                    req.getRequestDispatcher("fail.jsp").forward(req, resp);
                }
                break;

            case "toUpdatePage":

                //去修改页面
                req.getRequestDispatcher("update.jsp").forward(req, resp);
                break;
            case "update":
                boolean update = userinfoservice.updateUser(paramMap);
                if (update) {
                    //成功
                    findAll(req, resp);
                } else {
                    //失败
                    req.getRequestDispatcher("fail.jsp").forward(req, resp);
                }
                break;
            case "findUserByid":
                Object obj = paramMap.get("id");
                int MyId = Integer.parseInt(obj + "");
                Map<String, Object> user = userinfoservice.findUserById(MyId);
                System.out.println(user.toString());
                 String username= user.get("username")+"";
                //将内容响应回浏览器
                resp.setContentType("text/html");
                 resp.getWriter().print(username);
                break;
            case "findUserByUsername":
                String usernameNew = paramMap.get("username")+"";
                boolean checkName= userinfoservice.findUserByUsername(usernameNew);
                //返回json格式数据
                Map map = new HashMap();
                map.put("check",checkName);
                //将map转换成就json字符串
                String jsonString = JSON.toJSONString(map);
                resp.setContentType("application/json");
                 resp.getWriter().print(jsonString);
                break;
            default:
                System.out.println("查询操作");
                findAll(req, resp);
                break;
        }


    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有的人员信息
        List<Map<String, Object>> allUser = userinfoservice.findAllUser();
        //将所有的人员信息放在session中
        req.getSession().setAttribute("allUser", allUser);
        req.getRequestDispatcher("showUser.jsp").forward(req, resp);
    }
}

